package Dao;

import common.Util;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 通过这个类 来封装对于 Blog 表的操作
 * 1. 新增一个博客
 * 2. 获取到所有博客列表
 * 3. 根据博客 id 获取到指定的博客
 * 4. 根据博客 id 删除博客
 */
public class BlogDao {
    public void insert(Blog blog) {
        System.out.println("插入一个博客");

        Connection connection = null;
        PreparedStatement statement = null;
        try{
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 构造 SQL 语句
            String sql = "insert into blog values (null,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setTimestamp(3,blog.getPostTime());
            statement.setInt(4,blog.getUserId());
            // 3. 执行sql
            int ret = statement.executeUpdate();
            if(ret == 1){
                System.out.println("插入成功!");
            }else {
                System.out.println("插入失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭释放资源
            DBUtil.close(connection,statement,null);
        }
    }

    public List<Blog> selectAll() {
        System.out.println("获取所有的博客内容");

        List<Blog> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try{
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 构造sql语句
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            // 3. 执行 sql 语句
            resultSet = statement.executeQuery();
            // 4. 遍历 结果集
            while(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content =resultSet.getString("content");
                if(content.length() > 90){
                    content = content.substring(0,90)+ " ...";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                list.add(blog);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            // 5. 关闭释放资源
            DBUtil.close(connection,statement,resultSet);
        }
        return list;
    }

    public List<Blog> selectAllPerson(int userId) {
        System.out.println("获取个人所有的博客");

        List<Blog> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try{
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼接 SQL 语句
            String sql = "select * from blog where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);
            // 3. 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 4. 遍历结果集
            while(resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content =resultSet.getString("content");
                if(content.length() > 90){
                    content = content.substring(0,90)+ " ...";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                list.add(blog);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return list;
    }

    public Blog selectOne(int blogId) {
        System.out.println("获取指定的博客内容");

        Blog blog = new Blog();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try{
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 sql 语句
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            // 3. 执行 sql 语句
            resultSet = statement.executeQuery();
            // 4. 遍历 结果集
            while(resultSet.next()){
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 5. 关闭释放资源
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    public Integer selectUserId(int blogId){
        System.out.println("通过blogId获取userId");
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 SQL 语句
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            // 3. 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 4. 遍历结果集
            if(resultSet.next()){
                return resultSet.getInt("userId");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
    public void delete(int blogId){
        System.out.println("删除指定的博客内容");
        Connection connection = null;
        PreparedStatement statement = null;
        try{
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 sql 语句
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            // 3. 执行 sql 语句
            int ret = statement.executeUpdate();
            if(ret == 1){
                System.out.println("删除成功!");
            }else {
                System.out.println("删除失败!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            // 4. 关闭释放资源
            DBUtil.close(connection,statement,null);
        }
    }

    public Integer selectTotal(int userId){
        System.out.println("计算自己所有的文章");
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 SQL 语句
            String sql = "select count(userId) from blog where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);
            // 3. 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 4. 遍历结果集
            if(resultSet.next()){
                return resultSet.getInt(userId);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    public static void main(String[] args) {
        BlogDao blogDao = new BlogDao();
        System.out.println(blogDao.selectTotal(0));
    }
}
